import { HttpResponse } from 'msw';
import type { HttpHandler } from 'msw';
import { faker } from '@faker-js/faker';
import { apiHttp, mockStructure } from '../..';



const userInfo = {
  avatar: faker.image.avatar(),
  name: faker.person.fullName(),
  email: faker.internet.email(),
  phone: faker.phone.number(),
};

let role = '';

export const handlers: HttpHandler[] = [
  // POST 请求示例
  apiHttp.post('/login', async ({ request }) => {
    const credentials = (await request.json()) as { username: string; password: string };

    if (credentials.username === 'normalization' && credentials.password === 'normalization8520') {
      role = 'admin';
      return HttpResponse.json(
        mockStructure.ok(
          {
            token: 'admin',
            userInfo: {
              ...userInfo,
              id: role === 'admin' ? 1 : 2,
              role,
            },
          },
          '登录成功,欢迎回家!!!'
        )
      );
    } else if (credentials.username === 'role1' && credentials.password === 'role123') {
      role = 'role1';
      return HttpResponse.json(
        mockStructure.ok(
          {
            token: 'role1',
            role,
            userInfo: {
              ...userInfo,
              id: role === 'admin' ? 1 : 2,
              role,
            },
          },
          '登录成功,欢迎回家!!!'
        )
      );
    } else if (['normalization', 'role1'].includes(credentials.username)) {
      return HttpResponse.json(mockStructure.error('用户名或密码错误'));
    } else {
      return HttpResponse.json(mockStructure.error('Invalid credentials'), { status: 401 });
    }
  }),

  // 带参数的请求
  apiHttp.get('/user/info', (req) => {
    console.log(req.request.headers);

    // const token = req.headers.get('Authorization');
    // console.log(token);

    return HttpResponse.json(
      mockStructure.ok({
        userInfo: {
          id: role === 'admin' ? 1 : 2,
          role,
          ...userInfo,
        },
      })
    );
  }),
  apiHttp.get('/menu', () => {
    return HttpResponse.json(mockStructure.ok(menuList));
  }),
];

interface MenuResponse {
  id: number;
  name: string;
  pid?: number;
  path?: string;
  component?: string;
  redirect?: string;
  meta?: Record<string, any>;
  alwaysShow?: boolean;
  keepAlive?: boolean;
  children?: MenuResponse[];
}
const menuList: MenuResponse[] = [
  {
    id: 1,
    name: 'main',
    path: '/main',
    pid: 0,
    redirect: '/main/board',
    meta: {
      title: '菜单',
      icon: 'icon-main',
    },
    alwaysShow: true,
    keepAlive: false,
  },
  {
    id: 2,
    name: 'board',
    path: '/main/board',
    pid: 1,
    redirect: '',
    meta: {
      title: '控制台',
      icon: 'icon-board',
    },
    alwaysShow: true,
    keepAlive: false,
  },
  {
    id: 3,
    name: 'role',
    path: '/main/role',
    pid: 1,
    redirect: '/main/role/addRole',
    meta: {
      title: '角色管理',
      icon: 'icon-board',
    },
    alwaysShow: true,
    keepAlive: false,
  },
  {
    id: 31,
    name: 'addRole',
    path: '/main/role/addRole',
    pid: 3,
    meta: {
      title: '添加角色',
      icon: 'icon-board',
    },
    alwaysShow: true,
    keepAlive: false,
  },
  {
    id: 32,
    name: 'roleList',
    path: '/main/role/list',
    pid: 3,
    meta: {
      title: '角色列表',
      icon: 'icon-board',
    },
    alwaysShow: true,
    keepAlive: false,
  },
  {
    id: 20,
    name: 'main1',
    path: '/main1',
    pid: 0,
    redirect: '/main1/board',
    meta: {
      title: '菜单',
      icon: 'icon-main',
    },
    alwaysShow: true,
    keepAlive: false,
  },
  {
    id: 21,
    name: 'board1',
    path: '/main1/board',
    pid: 20,
    redirect: '',
    meta: {
      title: '控制台',
      icon: 'icon-board',
    },
    alwaysShow: true,
    keepAlive: false,
  },
  {
    id: 300,
    name: 'role1',
    path: '/main1/role',
    pid: 20,
    redirect: '/main1/role/addRole',
    meta: {
      title: '角色管理',
      icon: 'icon-board',
    },
    alwaysShow: true,
    keepAlive: false,
  },
  {
    id: 301,
    name: 'addRole1',
    path: '/main1/role/addRole',
    pid: 300,
    meta: {
      title: '添加角色',
      icon: 'icon-board',
    },
    alwaysShow: true,
    keepAlive: false,
  },
];
